package com.ctw.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    private Object lock1 = new Object();

    private Object lock2 = new Object();

    @RequestMapping("/test")
    public String test() {
        System.out.println("mybatis-jenkins");
        return "mybatis-jenkins2";
    }

    @RequestMapping("/cpuHihgher")
    public String cpuHihgher() throws InterruptedException {
        int i = 0;
        while (i == 0) {

        }
        return "mybatis-jenkins2";
    }


    @RequestMapping("/deadLock")
    public String deadLock() {
        new Thread(() -> {
            synchronized (lock1) {
                sleep(1000);
                synchronized (lock2) {

                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock2) {
                sleep(200);
                synchronized (lock1) {

                }
            }
        }).start();
        return "产生死锁";
    }

    private void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
